package cn.zymall.merchant.service.impl;

import cn.zymall.common.model.Coupon;
import cn.zymall.merchant.mapper.MerchantCouponMapper;
import cn.zymall.merchant.service.MerchantCouponService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName MerchantCouponServiceImpl
 * @Description TODO
 * @Author chenmei
 * @Date 2020/8/24 14:33
 * @Version 1.0
 **/
@Service
public class MerchantCouponServiceImpl extends ServiceImpl<MerchantCouponMapper, Coupon> implements MerchantCouponService {

	/**
	 *获取所有的优惠卷
	 * @Author: chenmei
	 * @param
	 * @Date: 2020/8/24 14:36
	 * @return:
	 **/
	@Override
	public List<Coupon> getAllCoupon() {
		return baseMapper.getAllCoupon();
	}

	/**
	 * 修改优惠卷的状态
	 * @Author: chenmei
	 * @param
	 * @Date: 2020/8/24 17:52
	 * @return:
	 **/
	public void updataCouponStatus(Integer coupon ,Integer couponStatus){
		UpdateWrapper<Coupon> updateWrapper = new UpdateWrapper<>();
		updateWrapper.eq("COUPON_ID",coupon).set("COUPON_STATUS",couponStatus);
		baseMapper.update(null,updateWrapper);
	}


	/**
	 * 根据优惠卷id获取单条优惠卷
	 * @Author: chenmei
	 * @param couponId  优惠卷id
	 * @Date: 2020/8/24 17:52
	 * @return:
	 **/
	public Coupon getCouponById(Integer couponId ){
		return baseMapper.selectById(couponId);
	}

	/**
	 * 优惠卷的联合查询
	 * @Author: chenmei
	 * @param couponName
	 * @param goodsType
	 * @param couponStatus
	 * @Date: 2020/8/25 8:15
	 * @return
	 */
	@Override
	public List<Coupon> conditionQuery( String couponName,  Integer goodsType, Byte couponStatus){
		return baseMapper.conditionQuery(couponName,goodsType,couponStatus);
	}

	/**
	 * 获取店铺下一张正常优惠卷信息
	 * @Author: chenmei
	 * @param merchantId 店铺id
	 * @Date: 2020/8/25 11:24
	 * @return:
	 **/
	@Override
	public Coupon getMerchantSingleCoupon(Integer merchantId) {
		return baseMapper.getMerchantSingleCoupon(merchantId);
	}

	/**
	 * 获取店铺下的所正常优惠卷
	 * @Author: chenmei
	 * @param merchantId 店铺id
	 * @Date: 2020/8/25 20:16
	 * @return:
	 **/
	@Override
	public List<Coupon> getCouponByMerchantId(Integer merchantId){
		QueryWrapper<Coupon> qw= new QueryWrapper<>();
		qw.eq("MERCHANT_ID",merchantId).eq("COUPON_STATUS",0).gt("REMAINDER",0);

		return baseMapper.selectList(qw);
	}

	/**
	 * 优惠卷余数减1
	 * @Author: chenmei
	 * @param couponId	 优惠卷id
	 * @Date: 2020/8/26 7:56
	 * @return:
	 **/
	@Override
	public Integer updateMerchantCouponNum(Integer couponId){
		Coupon cou=baseMapper.selectById(couponId);
		Integer remainder=cou.getRemainder();
		UpdateWrapper<Coupon> uw=new UpdateWrapper<>();
		uw.eq("COUPON_ID",couponId).set("REMAINDER",remainder-1);
		return baseMapper.update(null,uw);
	}
}
